package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.MqAlarmResult;
import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.LocationUtils;
import com.wtwd.campus.utils.UnicodeUtil;
import org.apache.commons.lang.StringUtils;

import java.util.List;

public class SetAlarmReceiver implements Receiver {
    @Override
    public String parseMessage(String data) {
        //*TJ,IMEI,V4, ALARMCLOCK,HHMMSS,sequence,name,time，repeatTime，flag,DeviceTime,A,2212.8745,N,11346.6574,E,14.28,028,DeviceDate,FFFFFFFF
        String[] split = data.split(",");
        String imei = split[1];
        String messageType = split[2];
        String type = split[3];
        String commandNumber = split[4];
        String sequence = split[5];
        String name = split[6];
        if (!StringUtils.isBlank(name)) {
            name = UnicodeUtil.decodeUnicode(name);
        }
        String time = split[7];
        String repeatTime = split[8];
        String flag = split[9];
        String deviceTime = split[10];
        String latitude = split[12];
        String longitude = split[14];
        String speed = split[16];
        String orientation = split[17];
        String deviceDate = split[18];
        List<Double> list = LocationUtils.transiformLatAndLng(longitude, latitude);
        MqAlarmResult result = new MqAlarmResult();
        result.setCommandNumber(commandNumber);
        result.setDeviceDate(deviceDate);
        result.setDeviceTime(deviceTime);
        result.setFlag(Integer.valueOf(flag));
        result.setImei(imei);
        result.setName(name);
        result.setRepeatTime(repeatTime);
        result.setTime(time);
        result.setSequence(Integer.valueOf(sequence));
        MqLocationData mqLocationData = new MqLocationData();
        mqLocationData.setOrientation(Integer.parseInt(orientation));
        mqLocationData.setSpeed(Double.parseDouble(speed));
        mqLocationData.setLatitude(list.get(1));
        mqLocationData.setLongitude(list.get(0));
        result.setMqLocationData(mqLocationData);
        InjectFeignService.getIrmFeignClientService().setAlarmReply(result);
        return imei;
    }
}
